package com.wdg.common.reponsitory;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;

/**
 * 实现最基本的增删改查接口 在IBaseDao中
 * @param <T>
 * @param <ID>
 */
public abstract class DataJpaBaseDao<T, ID extends Serializable> extends BaseDao  implements IBaseDao<T, ID> {
    private CrudRepository<T, ID> dataJpaRepository;

    public DataJpaBaseDao(CrudRepository<T, ID> dataJpaRepository) {
        this.dataJpaRepository = dataJpaRepository;
    }

    public <S extends T> S save(S entity) {
        return this.dataJpaRepository.save(entity);
    }

    public <S extends T> S update(S entity) {
        return this.dataJpaRepository.save(entity);
    }

    public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
        return this.dataJpaRepository.saveAll(entities);
    }

    public <S extends T> Iterable<S> updateAll(Iterable<S> entities) {
        return this.dataJpaRepository.saveAll(entities);
    }

    public Optional<T> getById(ID id) {
        return this.dataJpaRepository.findById(id);
    }

    public boolean existsById(ID id) {
        return this.dataJpaRepository.existsById(id);
    }

    public Iterable<T> findAllById(Iterable<ID> ids) {
        return this.dataJpaRepository.findAllById(ids);
    }

    public void deleteById(ID id) {
        this.dataJpaRepository.deleteById(id);
    }

    public void delete(T entity) {
        this.dataJpaRepository.delete(entity);
    }

    public void deleteAll(Iterable<ID> ids) {
        if (ids != null && ids.iterator().hasNext()) {
            Iterator var2 = ids.iterator();

            while(var2.hasNext()) {
                ID id = (ID) var2.next();
                this.deleteById(id);
            }
        }
    }
}
